<?php 
session_start();
include "../_init_.php";
include(constant('PATH_INCLUDE').'/class.uploadhelper.php');

$dtRentImage = new DataTable(constant('DB_TABLE_RENT_IMAGE'));
//增加数据
$action= isset($_REQUEST["action"])?$_REQUEST["action"]:'';
switch($action)
{
	case 'save':
		$data = $_REQUEST;
		try
		{
			$is_new = true;
			$data = checkData($data);
			if(isset($data['id']) && intval($data['id']) > 0)
				$is_new = false;
			$dtRentImage->save($data);
			if($is_new)
				$sucess_message = '新增房源圖片成功';
			else
				$sucess_message = '修改房源圖片成功';
		}catch(Exception $ex)
		{
			$action = 'edit';
			$info = $data;
			$error_message = $ex->getMessage();
		}
	break;
	case 'delete':
		//delete image
		$current_data = $dtRentImage->get($_REQUEST['id']);
		$img_path = constant('SITE_WEB_DISK_DIR').$current_data['image'];
		unlink($img_path);
		$dtRentImage->remove($_REQUEST['id']);
		$sucess_message = '刪除房源圖片成功';
	break;
	case 'moveup':
		$rowData = $dtRentImage->get($_REQUEST['id']);
		$query = sprintf("SELECT `sn` FROM %s WHERE `rId`=%d AND `sn` < %d ORDER BY `sn` DESC LIMIT 0,1",constant('DB_TABLE_RENT_IMAGE'),$_REQUEST['rId'],$rowData['sn']);
		$result = mysql_query($query);
		if($result === false)
		{
			echo mysql_error().__LINE__;
		}
		if($row = mysql_fetch_assoc($result))
		{
			$rowData['sn'] = $row['sn'] - 1;
		}else{
			$rowData['sn'] = $rowData['sn'] - 1;
		}
		$dtRentImage->save($rowData);
		
	break;
	case 'movedown':
		$rowData = $dtRentImage->get($_REQUEST['id']);
		$query = sprintf("SELECT `sn` FROM %s WHERE `rId`=%d AND `sn` > %d ORDER BY `sn` ASC LIMIT 0,1",constant('DB_TABLE_RENT_IMAGE'),$_REQUEST['rId'],$rowData['sn']);
		$result = mysql_query($query);
		if($result === false)
		{
			echo mysql_error().__LINE__;
		}
		if($row = mysql_fetch_assoc($result))
		{
			$rowData['sn'] = $row['sn'] + 1;
		}else{
			$rowData['sn'] = $rowData['sn'] + 1;
		}
		$dtRentImage->save($rowData);
	break;
}

function checkData($data)
{
	//upload image
	try
	{
		$uploadDir = '/upload/';
		$path = UploadHelper::uploadImage('image',$uploadDir);
		$data['image'] = $path;
	}catch(Exception $ex)
	{
	}
	return $data;
}


?>



<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>網站管理系統</title>


<LINK REL="stylesheet" HREF="css/style.css" TYPE="text/css">
<LINK REL="stylesheet" HREF="css/main.css" TYPE="text/css">
<script src="js/FancyZoom.js" language="JavaScript" type="text/javascript"></script>
<script src="js/FancyZoomHTML.js" language="JavaScript" type="text/javascript"></script>
<body onLoad="setupZoom();">

<script language="javascript">
<!--

function ConfirmDel()
{
   if(confirm("確定要刪除選擇的項目嗎？一旦刪除將不能恢複！"))
     return true;
   else
     return false;	 
}


</script>

<script language="javascript">
	function check(form1){
		if(form1.image.value==""){
			alert("請選擇圖片!");
			return false;		
		}
	}
</script>
<style>
.rentimagegallery
{
}
.rentimagegallery div
{	
float:left;
margin:5px;
}
.rentimagegallery .img
{
	border:1px solid #888888;
	padding:2px;
}
</style>
<table width="98%" border="0" class="table2">
  <tr>
    <td>系統管理 - 管理員管理</td>
  </tr>
</table>
<?php if($error_message):?>
<div class="error_message"><?php echo $error_message;?></div>
<?php endif;?>
<?php if($sucess_message):?>
<div class="success_message"><?php echo $sucess_message;?></div>
<?php endif;?>
<table width="100%"  border="0" cellpadding="0" cellspacing="0">
 <tr> 
          <td  height="20" class="back_southidc" >
          <div id="tabnav">
          	<ul>
          		<li><a href="rent_info.php?id=<?php echo $_REQUEST['rId'];?>&action=edit">房源档案</a></li>
          		
          		<li><a href="rent_image.php?rId=<?php echo $_REQUEST['rId'];?>">圖片</a></li>
          		
          	</ul>
          </div>
          </td>
        </tr>
  <tr> 
    <td align="center" valign="top">
      </strong> 
      <table width="100%" border="0" cellpadding="2" cellspacing="1" class="table_southidc">
        <tr> 
          <td width="100%"  height="20" class="back_southidc" > <div align="center"><strong>房源圖片管理</strong></div></td>
        </tr>
        <tr class="tr_southidc"> 
          <FORM   method="post" name="form1"  onsubmit="return check(this);" enctype="multipart/form-data" >	
          <input type="hidden" name="action" value="save"/>
          <input type="hidden" name="rId" value="<?php echo $_REQUEST['rId'];?>"/>
            <td bgcolor="#B0DAFF" align="left">
            <table  border="0" align="left" cellspacing="1" >
                
                 <tr> 
                  <td  height="22" > <div align="right">圖片：</div></td>
                  <td  ><input name="image" type="file"  size="15" maxlength="20"></td>
                   <td> <INPUT type=submit    name=Submit2 value="新增"/></td>
                </tr>
              
				
             
            </table></td>
          </form>
        </tr>
      </table>
      </td>
      <tr><td>
      <table width="820" border="0" cellpadding="2" cellspacing="1" class="table_southidc" style="background-color:#FFFFFF;">
       	  <tr>
       	  <td>
       	  	<div class="rentimagegallery">
       	  	<?php
       	  		$condition = sprintf("`rId`=%d",$_REQUEST['rId']);
       	  		$data_list = $dtRentImage->get_list($condition,'sn');
       	  	?>
       	  	<?php foreach($data_list as $row):?>
       	  	<div>
       	  	<table>
       	  		<tr><td> 	
       	  			<image class="img" src="<?php echo constant('SITE_URL_PATH').$row['image'];?>" width="150" height="130"/>
       			</td></tr>
       	  		<tr><td>
       	  		<input type="button" value="刪除" onclick="if(confirm('確定要刪除選擇的項目嗎？一旦刪除將不能恢複')){document.location='?rId=<?php echo $_REQUEST['rId'];?>&action=delete&id=<?php echo $row['id'];?>';}"/>&nbsp;
       	  		<input type="button" value="上移" onclick="document.location='?rId=<?php echo $_REQUEST['rId'];?>&action=moveup&id=<?php echo $row['id'];?>';"/>&nbsp;
       	  		<input type="button" value="下移" onclick="document.location='?rId=<?php echo $_REQUEST['rId'];?>&action=movedown&id=<?php echo $row['id'];?>';"/>
       	  			</td></tr>
       	  	</table>
       	  	</div>
       	   	<?php endforeach;?>
       	  	</div>
       	  	</td>
       	  </tr>
      </table>
    </td>
  </tr>
</table>
<?php  include("foot.php");?>